<?php


namespace App\controllers\admin;


use App\controllers\Base;
use Consts\RedisKey;
use Helper\Config;
use Helper\Output;
use Helper\Mysql;
use Firebase\JWT\JWT;
use Helper\Redis;
use Helper\Reg;
use Middleware\AuthMiddleware;

class User extends Base
{

    public function index($request, $response){


        $mysql = Mysql::getInstance()
                ->join('role as r','au.role_id=r.id','left')
                ->where('platform_id',$request->post['platform_id']);
        $list = $mysql->where('role_id',1,'<>')
            ->get('admin_user as au',null,
                'au.id,nick,username,role_id,problem_ids,platform_id,phone,avatar,is_online,wechat,qq,email,au.state,max_service_num,r.name'
            );

        return Output::json(200,$list);

    }

    public function create($request, $response){

        $data = $request->post;
        $data['create_at'] = time();
        $data['password'] = md5(  $data['password']);
        if($request->files){
            $file_name = $this->uploadImgs($request ,'avatar','imgs');
            if($file_name ){
                $data['avatar'] = $file_name;
            }

        }
        //检测账号是否存在
        $isexist = Mysql::getInstance()->where('username',$data['username'])->getOne('admin_user');
        if($isexist){
            return Output::json(1033);
        }
        $res = Mysql::getInstance()->insert('admin_user',$data);
        if($res){
            $id = Mysql::getInstance()->getInsertId();
            //更新用户信息
            Redis::getInstance()->hSet(RedisKey::ADMIN_SERVICE_INFO_LIST,'id:'.$id,json_encode(Mysql::getInstance()->where('id',$id)->getOne('admin_user')));
            return Output::json(200);
        }
        return Output::json(400);
    }


    public function edit($request, $response){

        $data = $request->post;
        $id = $request->post['id'];
        unset($data['id']);
        if($data['password']){
            $data['password'] = md5(  $data['password']);
        }
        //防止超级管理员给自己权限降级了
        if($this->getRoleId() == 1 && $id == $this->getUserId()){
            unset($request->post['role_id']);
            unset($request->post['platform_id']);

        }else{

            Redis::getInstance()->set(RedisKey::user_authority_list($id),
                json_encode(AuthMiddleware::getInstance()->getAuthority($request->post['role_id']))
            );
        }

        if($request->files){
            $file_name = $this->uploadImgs($request ,'avatar','imgs');
            if($file_name ){
                $data['avatar'] = $file_name;
            }

        }

        $res = Mysql::getInstance()->where('id',$id)->update('admin_user',$data);

        if($res){
            //更新用户信息
            Redis::getInstance()->hSet(RedisKey::ADMIN_SERVICE_INFO_LIST,'id:'.$id,json_encode(Mysql::getInstance()->where('id',$id)->getOne('admin_user')));
            return Output::json(200);
        }
        return Output::json(400);
    }


    public function editInfo($request, $response){

        $data = $request->post;
        $id = $request->post['id'];
        if($data['password']){
            $data['password'] = md5(  $data['password']);
        }
        if($request->files){
            $file_name = $this->uploadImgs($request ,'avatar','imgs');
            if($file_name ){
                $data['avatar'] = $file_name;
            }

        }
        $res = Mysql::getInstance()->where('id',$id)->update('admin_user',$data);
        if($res){
            return Output::json(200);
        }
        return Output::json(400);

    }

}